unit AfterDownload_Log;

interface

uses
 gnugettext,
 JvLogFile;

const
 LogFilename = 'AfterDownload.log';

 //log settings
 cLogUse = $1000;
 cLogFileSaved = $001;
 cLogMatched = $0002;
 cLogUnmatched = $0004;
 cLogActions = $0008;
 cLogActionErrors = $0010;
 cLogChanged = $0020;
 cLogAll = $0FFF;

 cTimeExit = 0;
 cTime60 = 1;
 cTime10 = 2;
 cTime05 = 3;
 cTimeEvery = 4;

 //log strings
function cLogFile: String;
function cLogRule: String;
function cLogAction: String;
function cLogError: String;
//
function IsBitSet(Value, BitPlace: Integer): Boolean;
function IsBitsSet(Value: Integer; const Bits: array of const): Boolean;
//
procedure LogCreate(const LoadFrom: String; SaveAuto: Boolean = false);
procedure LogSave(SaveTo: String = '');
procedure LogShow(Title: String);
procedure LogFree;
function LogActive: Boolean;
procedure Log(AReason, AText: String; TimeOnly: Boolean = false);

var
 LogFolder: String = '';
 LogTitle: String = '';

implementation

uses
 SysUtils,
 Wizard,
 JvLogClasses;

var
 LogFile: TJvLogFile = nil;

function cLogFile: String;
begin
 Result:=_('File');
end;

function cLogRule: String;
begin
 Result:=_('Rule');
end;

function cLogAction: String;
begin
 Result:=_('Action');
end;

function cLogError: String;
begin
 Result:=_('Error');
end;

 {Log operation routines}

function IsBitSet(Value, BitPlace: Integer): Boolean;
begin
 Result:=(Value and BitPlace)>0;
end;

function IsBitsSet(Value: Integer; const Bits: array of const): Boolean;
 var
  i: Integer;
begin
 Result:=true;
 for i:=0 to High(Bits) do
  begin
   if Bits[i].VType=vtInteger then
     Result:=Result and IsBitSet(Value, Bits[i].VInteger);
  end;
end;

procedure LogCreate(const LoadFrom: String; SaveAuto: Boolean = false);
begin
 LogFree;
 LogFile:=TJvLogFile.Create(nil);
 LogFile.DefaultSeverity:=lesInformation;
 LogFile.AutoSave:=SaveAuto;
 if FileExists(LoadFrom) then
   LogFile.Filename:=LoadFrom;
end;

procedure LogSave(SaveTo: String = '');
begin
 if LogActive then
  begin
   if SaveTo='' then
     LogFile.SaveToFile(LogFile.FileName)
   else
     LogFile.SaveToFile(SaveTo);
  end;
end;

procedure LogShow(Title: String);
begin
 if LogActive then
   LogFile.ShowLog(Title);
end;

procedure LogFree;
begin
 if LogActive then
  begin
   FreeAndNil(LogFile);
  end;
end;

function LogActive: Boolean;
begin
 Result:=Assigned(LogFile);
end;

procedure Log(AReason, AText: String; TimeOnly: Boolean = false);
 var
  LogText: String;
begin
 if LogActive then
  begin
   LogText:=AText;
   ReplaceEx(LogText, #13, '_');
   ReplaceEx(LogText, #10, '_');
   ReplaceEx(LogText, '<', '(');
   ReplaceEx(LogText, '>', ')');
   if TimeOnly then
     LogFile.Add(TimeToStr(Time), AReason, LogText)
   else
     LogFile.Add(DateTimeToStr(Now), AReason, LogText);
  end;
end;

end.
 